//
//  InformationViewController.m
//  CivilianTravel
//
//  Created by 许仕杰 on 16/7/20.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "InformationViewController.h"
#import "XSJScrollView.h"
#import "FoodViewController.h"
#import "HomeTravelViewController.h"

#define KRatioW   self.view.frame.size.width / 414
#define KRatioH   self.view.frame.size.height / 736
@interface InformationViewController ()<UIWebViewDelegate>


/** view */
@property (nonatomic, strong) UIScrollView *headView;

/** 内容 */
@property (nonatomic, strong) NSString *desc;
/** 详细信息 */
@property (nonatomic, strong) NSString *summary;
/** 图片网站 */
@property (nonatomic, strong) NSMutableArray *contentArray;

/** 网页内容 */
@property (nonatomic, strong) UIWebView *contentWebView;

/** 路由视角 */
@property (nonatomic, strong) UIWebView *friendWebView;

/**  */
@property (nonatomic, strong) UILabel *trafficLabel;

/** 个数 */
@property (nonatomic, assign) NSInteger index;

@end

@implementation InformationViewController

- (UILabel *)trafficLabel{
    if (!_trafficLabel) {
        _trafficLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*KRatioW, CGRectGetMaxY(self.friendWebView.frame)+5*KRatioH, 100*KRatioW, 50*KRatioH)];
    }
    return _trafficLabel;
}



- (NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}

- (UIScrollView *)headView{
    if (!_headView) {
        _headView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    }
    return _headView;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    [self requestInformationWithCityID:self.cityID];
    [self loadContent];
    [self.view addSubview:self.headView];
    self.friendWebView.userInteractionEnabled = NO;
    self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
}
//数据请求
- (void)requestInformationWithCityID:(NSString *)cityID{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/city/base" parameter:@{@"a_t":@"1469065630000",@"token":@"578624ce19a3f",@"cityid":cityID,@"sign":@"9bf05708ac377cd3400d14cbf8a7586590684a0e"} method:@"post" compeleted:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        self.index = [dic[@"data"] count];
        if ([dic[@"data"] count] == 0) {
            [SVProgressHUD show];
        } else {
                self.desc = dic[@"data"][@"desc"];
            self.summary = dic[@"data"][@"summary"];
            NSSet *avatarlist = dic[@"data"][@"avatarlist"];
            for (NSDictionary *url in avatarlist) {
                [self.contentArray addObject:url[@"url"]];
            }
        }
    } updateUI:^{
        if (self.index == 0) {
            
        } else {
            XSJScrollView *scrollView = [[XSJScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight*1/4.0) imageArray:self.contentArray];
            [self.friendWebView loadHTMLString:self.desc baseURL:nil];
            [self.contentWebView loadHTMLString:self.summary baseURL:nil];
            [self.headView addSubview:scrollView];
        }
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 加载内容
- (void)loadContent{
    /** 美食按钮 */
    UIButton *foodBtn = [[UIButton alloc] initWithFrame:CGRectMake(KRatioW*40, 1/4.0*kHeight+KRatioW*10, 1/4.0*kWidth, 50*KRatioH)];
    [foodBtn setTitle:@"美食" forState:UIControlStateNormal];
    [foodBtn addTarget:self action:@selector(footAction) forControlEvents:UIControlEventTouchUpInside];
    foodBtn.backgroundColor = [UIColor lightGrayColor];
    [self.headView addSubview:foodBtn];
    
    /** 景点按钮 */
    NSLog(@"%f",kWidth-1/4.0*kWidth-KRatioW*40);
    UIButton *spotsBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth-1/4.0*kWidth-KRatioW*40, 1/4.0*kHeight+KRatioW*10, 1/4.0*kWidth, 50*KRatioH)];
    [spotsBtn setTitle:@"景点" forState:UIControlStateNormal];
    [spotsBtn addTarget:self action:@selector(spotsAction) forControlEvents:UIControlEventTouchUpInside];
    spotsBtn.backgroundColor = [UIColor lightGrayColor];
    [self.headView addSubview:spotsBtn];
    
    /** 路友视角 */
    UILabel *friendLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*KRatioW, CGRectGetMaxY(foodBtn.frame)+5*KRatioH, 80*KRatioW, 50*KRatioH)];
    friendLabel.text = @"旅游功率";
    [self.headView addSubview:friendLabel];
    
    //线
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(friendLabel.frame), CGRectGetMinY(friendLabel.frame)+24*KRatioH, 2/3.0*kWidth, 1)];
    view1.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    [self.headView addSubview:view1];
    
    
    //城市内容
    self.friendWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10*KRatioW,CGRectGetMaxY(friendLabel.frame), kWidth-20*KRatioW, 100*KRatioH)];
    [self.headView addSubview:self.friendWebView];
    
    
    
    /** 交通 */
    UILabel *trafficLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*KRatioW, CGRectGetMaxY(self.friendWebView.frame)+5*KRatioH, 100*KRatioW, 50*KRatioH)];


    //
    self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10*KRatioW,CGRectGetMaxY(trafficLabel.frame), kWidth-20*KRatioW, 20*KRatioH)];
    self.friendWebView.delegate = self;
    self.contentWebView.delegate = self;
}

#pragma mark -- 点击事件
//美食
- (void)footAction{
    FoodViewController *food = [[FoodViewController alloc] init];
    food.cityID = self.cityID;
    [self.navigationController pushViewController:food animated:YES];
    
}

//景点
- (void)spotsAction{
    HomeTravelViewController *travel = [[HomeTravelViewController alloc] init];
    travel.cityID = self.cityID;
    [self.navigationController pushViewController:travel animated:YES];
}

#pragma mark --webViewdelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (webView == self.friendWebView) {
        CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
        NSLog(@"%f",height);
        CGRect webViewFrame = webView.frame;
        webViewFrame.size.height = height+10*KRatioH;
        webView.frame = webViewFrame;

        /** 交通 */
        self.trafficLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*KRatioW, CGRectGetMaxY(self.friendWebView.frame)+5*KRatioH, 100*KRatioW, 50*KRatioH)];
        self.trafficLabel.text = @"交通和服务";
        [self.headView addSubview:self.trafficLabel];
        
        //线
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.trafficLabel.frame), CGRectGetMinY(self.trafficLabel.frame)+24*KRatioH, 2/3.0*kWidth, 1)];
        view2.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
        [self.headView addSubview:view2];
    }
    if (webView == self.contentWebView) {
        CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
        NSLog(@"%f",height);
        CGRect webViewFrame = webView.frame;
        webViewFrame.size.height = height+10*KRatioH;
        webView.frame = webViewFrame;
        self.contentWebView.frame = CGRectMake(10*KRatioW,CGRectGetMaxY(self.trafficLabel.frame), kWidth-20*KRatioW, webView.frame.size.height*KRatioH);
        
        [self.headView addSubview:self.contentWebView];

        self.headView.contentSize = CGSizeMake(kWidth, CGRectGetMaxY(self.contentWebView.frame));
    }


}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");
}

@end
